package gui.pdf;

import gui.GUIFrame;
import helper.BLError;
import helper.BLWarning;

import java.awt.Cursor;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.net.URISyntaxException;

import javax.swing.JDialog;
import javax.swing.JLabel;

import org.xhtmlrenderer.pdf.ITextRenderer;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;

abstract class BasisPDF {
	
	protected Document document = null;

	protected String file = "";
	
	public abstract void createLayout() throws DocumentException, MalformedURLException, FileNotFoundException, com.lowagie.text.DocumentException, IOException, URISyntaxException, BLError, BLWarning;
	
	public void saveAsDialog() {
	}
	
	public void render() {
	}

	protected void save(String content) throws IOException, com.lowagie.text.DocumentException {
		OutputStream os = new FileOutputStream(this.file);
		
        JDialog test = new JDialog(GUIFrame.getInstance());
        test.setTitle("PDF wird erzeugt");
        test.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
        test.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
        JLabel l = new JLabel("Bitte warten...");
        test.setSize(500, 200);
        test.add(l);
        test.pack();
        test.setVisible(true);
        test.setAlwaysOnTop(true);
        
        ITextRenderer renderer = new ITextRenderer(); 
        renderer.setDocumentFromString(content);
        renderer.layout();
        renderer.createPDF(os);
        os.close();
        
        test.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
        
        test.dispose();
	}
	/**
	 * 
	 */
	public void setFile(String file) {
		this.file = file;
	}
	
}
